package com.pearadmin.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearadmin.common.nucleus.base.BaseController;
import com.pearadmin.common.nucleus.domain.request.PageParam;
import com.pearadmin.common.nucleus.domain.response.Result;
import com.pearadmin.common.nucleus.domain.response.TableVO;
import com.pearadmin.system.domain.SysPost;
import com.pearadmin.system.service.ISysPostService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
@RequestMapping("system/post")
public class SysPostController extends BaseController {

     private String MODULE_PATH = "system/post/";
     @Resource
     private ISysPostService sysPostService;

     @GetMapping("main")
     public ModelAndView main(ModelAndView modelAndView){
          modelAndView.setViewName(MODULE_PATH+"main");
          return modelAndView;
     }

     @GetMapping("data")
     public TableVO data(PageParam pageParam){
          IPage<SysPost> pageInfo = sysPostService.page(new Page<>(pageParam.getPage(),pageParam.getLimit()),null);
          return PageTable(pageInfo.getRecords(), pageInfo.getTotal());
     }

     @GetMapping("add")
     public ModelAndView add(ModelAndView modelAndView){
          modelAndView.setViewName(MODULE_PATH+"add");
          return modelAndView;
     }

     @PostMapping("save")
     public Result save(@RequestBody SysPost sysPost){
          boolean result = sysPostService.save(sysPost);
          return choice(result);
     }

     @GetMapping("edit")
     public ModelAndView edit(ModelAndView modelAndView,String id){
          SysPost sysPost = sysPostService.getById(id);
          modelAndView.addObject("sysPost",sysPost);
          modelAndView.setViewName(MODULE_PATH+"edit");
          return modelAndView;
     }

     @PutMapping("update")
     public Result update(@RequestBody SysPost sysPost){
          boolean result = sysPostService.updateById(sysPost);
          return choice(result,"修改成功","修改失败");
     }

     @PutMapping("enable/{id}")
     public Result enable(@PathVariable String id){
          SysPost sysPost = new SysPost();
          sysPost.setStatus("0");
          sysPost.setId(id);

          boolean result = sysPostService.updateById(sysPost);
          return choice(result);
     }

     @PutMapping("disable/{id}")
     public Result disable(@PathVariable String id){
          SysPost sysPost = new SysPost();
          sysPost.setStatus("1");
          sysPost.setId(id);

          boolean result = sysPostService.updateById(sysPost);
          return choice(result);
     }

}
